package com.wuzuqing.android.eightdiagrams.utils;

import java.util.Calendar;
 
public class NextJieqi {
    public static void main(String[] args) {
        // 创建 Calendar 对象并设置为当前日期
        Calendar calendar = Calendar.getInstance();
        
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1; // 注意月份从0开始计数，因此需要+1
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        
        System.out.println("当前日期：" + year + "年" + month + "月" + dayOfMonth + "日");
        
        String jieqiName = getNextJieqi(year, month, dayOfMonth);
        if (jieqiName != null) {
            System.out.println("下一个节气：" + jieqiName);
            
            // 输出下一个节气的日期
            int nextYear = Integer.parseInt(jieqiName.substring(2));
            int nextMonth = Integer.parseInt(jieqiName.charAt(3) + "");
            int nextDayOfMonth = Integer.parseInt(jieqiName.substring(4));
            
            System.out.println("下一个节气日期：" + nextYear + "年" + nextMonth + "月" + nextDayOfMonth + "日");
        } else {
            System.out.println("未知的节气或已经超过了最大支持范围");
        }
    }
    
    private static final String[] JIEQI_NAMES = {
            "立春", "雨水", "惊蛰", "春分", "清明", "谷雨",
            "立夏", "小满", "芒种", "夏至", "小暑", "大暑",
            "立秋", "处暑", "白露", "秋分", "寒露", "霜降",
            "立冬", "小雪", "大雪", "冬至"};
    
    /**
     * 根据传入的日期计算下一个节气名称
     */
    private static String getNextJieqi(int year, int month, int dayOfMonth) {
        for (int i = 0; i < JIEQI_NAMES.length - 1; i++) {
            int currentIndex = i % 2 == 0 ? i / 2 : (i - 1) / 2;
            int nextIndex = (currentIndex + 1) % 2 == 0 ? (currentIndex + 1) / 2 : ((currentIndex + 1) - 1) / 2;
            
            int currentYear = year + currentIndex;
            int nextYear = year + nextIndex;
            
            boolean isCurrentDateBeforeOrEqualToGivenDate = false;
            switch (month) {
                case 1:
                    isCurrentDateBeforeOrEqualToGivenDate = true;
                    break;
                case 2:
                    isCurrentDateBeforeOrEqualToGivenDate = dayOfMonth <= 5 && dayOfMonth >= 6;
                    break;
                default:
                    isCurrentDateBeforeOrEqualToGivenDate = dayOfMonth > 5 || dayOfMonth < 7;
                    break;
            }
            
            if (!isCurrentDateBeforeOrEqualToGivenDate) {
                return JIEQI_NAMES[nextIndex];
            }
        }
        
        return null;
    }
}